package com.tian.view.anim.attr_anim

import android.animation.Animator
import android.animation.ArgbEvaluator
import android.animation.FloatEvaluator
import android.animation.ValueAnimator
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import android.view.animation.BounceInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.tian.view.R
import com.tian.view.databinding.ViewFourBottonBinding

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/27 14:55
 *
 * Evaluator 是什么
 * 在动画中我们了解了 ValueAnimator 和 Interpolator.
 * ValueAnimator的值是怎么产生的呢？，看过程
 *
 * ValueAnimator 流程：
 *  ValueAnimator.ofInt(定义动画区间) --> Interpolator(返回当前进度eg:0.2) ---
 *  --> Evaluator(根据进度生成当前值)  ---> AnimatorUpdateListener(监听获取当前值).
 *
 *  我们了解了 Interpolator(插值器)返回的都是进度, 开始和结束分别是0、1，这不符合我们定义
 *  的动画区间的值，这里 Evaluator 就是根据插值器的进度计算当前的值，使返回值符合我们定义
 *  的动画区间，这个值就是我们从 AnimatorUpdateListener 中获取的值。
 *
 */
class EvaluatorFragment : Fragment() {

    private lateinit var b: ViewFourBottonBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        b = ViewFourBottonBinding.inflate(inflater)
        return b.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireActivity() as AppCompatActivity).supportActionBar?.title = "Evaluator"
        b.mbtOne.text = "默认Evaluator"
        b.mbtOne.setOnClickListener { startDefaultEvaluator() }

        b.mbtTwo.text = "自定义Evaluator"
        b.mbtTwo.setOnClickListener { startCustomEvaluator() }
        b.mbtThree.text = "颜色值ArgbEvaluator"
        b.mbtThree.setOnClickListener { argbEvaluator() }
        b.mbtFour.visibility = View.GONE
    }


    private fun startDefaultEvaluator() {
        val anim = ValueAnimator.ofFloat(0f, 600f)
        anim.duration = 2000L
        //延时多久才开始动画
        anim.startDelay = 500L
        anim.interpolator = BounceInterpolator()
        anim.addUpdateListener {
            val value = it.animatedValue as Float
            b.mbtOne.translationY = value
        }
        anim.repeatCount = 4
        anim.repeatMode = ValueAnimator.REVERSE
        /**
         * 这里其实是有默认的Evaluator, 在我们使用ValueAnimator.ofInt创建时就已赋值。
         */
        anim.setEvaluator(FloatEvaluator())
        anim.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator?) {

            }

            override fun onAnimationEnd(animation: Animator?) {
                b.mbtOne.translationY = 0f
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationRepeat(animation: Animator?) {
            }

        })
        anim.start()
    }

    private fun startCustomEvaluator() {
        val anim = ValueAnimator.ofFloat(0f, 600f)
        anim.duration = 2000L
        //延时多久才开始动画
        anim.startDelay = 500L
        anim.interpolator = AccelerateInterpolator()
        anim.addUpdateListener {
            val value = it.animatedValue as Float
            b.mbtTwo.translationY = value
        }
        anim.repeatCount = 4
        anim.repeatMode = ValueAnimator.REVERSE
        /**
         * 设置自定义的Evaluator
         */
        anim.setEvaluator(CustomEvaluator())
        anim.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator?) {

            }

            override fun onAnimationEnd(animation: Animator?) {
                b.mbtOne.translationY = 0f
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationRepeat(animation: Animator?) {
            }

        })
        anim.start()
    }

    private fun argbEvaluator(){
        val anim = ValueAnimator.ofInt(0xffffff00.toInt(), 0xff0000ff.toInt())
        anim.duration = 2000L
        anim.interpolator = AccelerateInterpolator()
        anim.addUpdateListener {
            val value = it.animatedValue as Int
            b.mbtThree.setBackgroundColor(value)
        }
        anim.repeatCount = 4
        anim.repeatMode = ValueAnimator.REVERSE
        /**
         * 设置自定义的Evaluator
         */
        anim.setEvaluator(ArgbEvaluator())
        anim.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator?) {

            }

            override fun onAnimationEnd(animation: Animator?) {
                b.mbtThree.setBackgroundResource(R.color.purple_500)
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationRepeat(animation: Animator?) {
            }

        })
        anim.start()
    }
}